<template>
  <div>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
	  <el-form-item label="角色编码">
        <el-input v-model="queryForm.code" placeholder="角色编码" clearable />
      </el-form-item>
      <el-form-item label="角色名称">
        <el-input v-model="queryForm.name" placeholder="角色名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'system:role:query'" type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-permission="'system:role:insert'" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="角色编码" align="center" prop="code" />
      <el-table-column label="角色名称" align="center" prop="name" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column>
        <template #default="scope">
		  <el-button v-permission="'sys:role:configPerm'" class="success" type="text" icon="Setting" @click="handleConfigPerms(scope.row)">配置权限</el-button>
          <el-button v-permission="'system:role:update'" link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-permission="'system:role:delete'" link type="primary" icon="Delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next, total" v-model:page-size="queryForm.pageSize" v-model:current-page="queryForm.pageNo" :total="total" @current-change="getList" />

    <EditForm v-on:callBack="getList" ref="editForm"></EditForm>
	<Perms v-on:callBack="getList" ref="perms"></Perms>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, nextTick, toRef } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import EditForm from './editform.vue';
import Perms from './perms';

// 查询参数
const queryForm = reactive({
  deptName: "",
  pageNo: 1,
  pageSize: 10
});
// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);
// 列表返回值
const list = ref<any[]>([]);

const editForm = ref(); //ref方法是让我们的简单类型数据变成响应式数据;在方法里改变时需要.value来获取

const perms = ref(); 

// 是否打开弹出框
const open = ref(false);
// 弹出框标题
const title = ref("");
// 提交表单数据
let form = toRef(
  reactive({
    deptId: "",
    deptName: "",
    leader: "",
    phone: "",
    email: ""
  })
);

/**
 * 查询列表
 */
const getList = () => {
  loading.value = true;
  baseService
    .get("/system/role/list", queryForm)
    .then((res) => {
      loading.value = false;
      if (res.code === 200) {
        list.value = res.rows;
        total.value = res.total;
      } else {
        list.value = [];
      }
    })
    .catch(() => {
      loading.value = false;
    });
};

/**
 * 搜索按钮操作
 */
function handleQuery() {
  queryForm.pageNo = 1;
  getList();
}





/**
 * 删除按钮操作
 * @param id 主键
 */
function handleDelete(id: any) {
  ElMessageBox.confirm("确认要删除当前项吗?", "提示").then(() => {
    baseService.delete(`/system/role/delete`, id).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

//新增按钮操作
const handleAdd = () => {
   editForm.value.addFormShow('新增角色')
};

//定义类型
interface Role {
  code: string
  name: string
  id: string
};

//编辑
const handleUpdate = (row: User) => {
  editForm.value.updateFormShow('修改角色', row)
};

//配置权限
const handleConfigPerms = (row: User) => {
  perms.value.permsFormShow('配置权限', row)
};

getList();
</script>

<style scoped></style>
